package com.tuananh.radnom.model.netpoller;

import java.util.HashMap;
import java.util.concurrent.Callable;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.sun.xml.txw2.annotation.XmlAttribute;
import com.tuananh.radnom.model.NetInterface;
import com.tuananh.radnom.model.netpoller.parser.AbstractParser;
import com.tuananh.radnom.model.netpoller.parser.PosixParser;
import com.tuananh.radnom.model.netservice.MyMapTypeAdapter;

@XmlRootElement(name = "net-poller")
public abstract class NetPoller {

	protected String netService;

	protected long timeout;

	protected String name;

	protected AbstractParser parser;

	@XmlElement(name = "poll-parser")
	public AbstractParser getParser() {
		if (parser == null)
			parser = new PosixParser();
		return parser;
	}

	public void setParser(AbstractParser parser) {
		this.parser = parser;
	}

	@XmlAttribute
	public String getNetService() {
		return netService;
	}

	public void setNetService(String netService) {
		this.netService = netService;
	}

	protected HashMap<String, String> params;

	@XmlAttribute
	public long getTimeout() {
		return timeout;
	}

	public void setTimeout(long timeout) {
		this.timeout = timeout;
	}

	@XmlJavaTypeAdapter(MyMapTypeAdapter.class)
	public HashMap<String, String> getParams() {
		if (params == null)
			params = new HashMap<String, String>();
		return params;
	}

	public void setParams(HashMap<String, String> params) {
		this.params = params;

	}

	@XmlAttribute
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	// public NetPoller clone() {
	// NetPoller egg;
	// try {
	// egg = cast(Class.forName(this.getClass().getName()).newInstance());
	// egg.params = (HashMap<String, String>) params.clone();
	// return egg;
	// } catch (InstantiationException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (IllegalAccessException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (ClassNotFoundException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return null;
	// }
	//
	// @SuppressWarnings("unchecked")
	// public static <T> T cast(Object o) {
	//
	// return (T) o;
	// }
	//
	// @Override
	// public String toString() {
	// // TODO Auto-generated method stub
	// String result = this.name.trim();
	// for (String key : params.keySet()) {
	// result += "\nkey=" + params.get(key);
	// }
	// return result;
	// }
	/** use to poll */
	public abstract String poll(PollInfo pollInfo, Object... args);

	public void populate(NetPollerConcrete poller) {
		this.timeout = poller.getTimeout();
		this.getParams().putAll(poller.getParams());
	}
}
